﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YY.BLL;

namespace BaseFunction.StaticTableClass
{
   public class agent_login_log_Help
    {
       public static YY.BLL.agent_login_log bll_agent_login_log = new agent_login_log();

       /// <summary>
       /// 根据用户名返回上一次登录记录
       /// </summary>
       public YY.Model.agent_login_log GetModel(string user_name, int top_num, string action_type)
       {
           int rows = GetCount("user_name='" + user_name + "'");
           if (top_num == 1)
           {
               rows = 2;
           }
           if (rows > 1)
           {
               var strSql = "";
               strSql += "select top 1 id from (select top " + top_num + " id from tb_agent_login_log";
               strSql += string.Format(@" where user_name='{0}' and action_type='{1}' order by id desc) as T ", user_name, action_type);
               strSql += " order by id asc";

               object obj = MsSql2008.GetSingle(strSql);
               if (obj != null)
               {
                   return new agent_login_log().GetModel(Convert.ToInt32(obj));
               }
           }
           return null;
       }
       /// <summary>
       /// 返回单条数据
       /// </summary>
       public int GetCount(string strWhere)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append("select count(*) as H from  tb_agent_login_log ");
           if (strWhere.Trim() != "")
           {
               strSql.Append(" where " + strWhere);
           }
           return Convert.ToInt32(MsSql2008.GetSingle(strSql.ToString()));
       }
    }
}
